package tx.bj12320.dao;

import java.sql.SQLException;
import java.util.List;

import javacommon.base.BaseHibernateDao;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;

import tx.bj12320.model.Infoissue;
import tx.bj12320.vo.query.InfoissueQuery;
import cn.org.rapid_framework.page.Page;

@Repository
public class InfoissueDao extends
		BaseHibernateDao<Infoissue, java.lang.Long> {

	public Class getEntityClass() {
		return Infoissue.class;
	}

	public Page findPage(InfoissueQuery query) {
		// [column]为字符串拼接, {column}为使用占位符.
		// [column]为使用字符串拼接,如username='[username]',偷懒时可以使用字符串拼接
		// [column] 为PageRequest的属性
		String sql = "select t from Infoissue t where 1=1 "
				+ "/~ and t.userInfoId = {userInfoId} ~/"
				+ "/~ and t.userInfo.orgInfo.orgName like '%[orgName]%' ~/"
				+ "/~ and t.infoTitle like '%[infoTitle]%' ~/"
				+ "/~ and t.issueDate >= {issueDateBegin} ~/"
				+ "/~ and t.issueDate <= {issueDateEnd} ~/"
				+ "/~ and t.infoContent = {infoContent} ~/"
				+ "/~ and t.attachmentName like '%[attachmentName]%' ~/"
				+ "/~ and t.attachmentFile = {attachmentFile} ~/"
				+ "/~ and t.infoType = {infoType} ~/"
				+ "/~ and t.isMail = {isMail} ~/"
				+ "/~ and t.mailReceiverId = {mailReceiverId} ~/"
				+ "/~ and t.mailReceiverType = {mailReceiverType} ~/"
				+ "/~ and t.replyInfoissueId = {replyInfoissueId} ~/"
				+ "/~ and t.isRead = {isRead} ~/"
				+ "/~ and t.readDate >= {readDateBegin} ~/"
				+ "/~ and t.readDate <= {readDateEnd} ~/"
				+ "/~ and t.mailReceiverSum = {mailReceiverSum} ~/"
				+ "/~ and t.isMainMail = {isMainMail} ~/"
				+ "/~ and t.mainMailInfoissueId = {mainMailInfoissueId} ~/"
				+ "/~ and t.isInfoToPublish = {isInfoToPublish} ~/"
				+ "/~ and t.isAdopt = {isAdopt} ~/"
				+ "/~ order by [sortColumns] ~/";

		return pageQuery(sql, query);

	}

	public List findByReciverInfo(final Long infoissue_ID) {
		return (List) getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				String sql = "SELECT  unit_name unit_name FROM  bjmdr_infoissue bjmdr_infoissue ,  bjmdr_report_unit_info  bjmdr_report_unit_info where  bjmdr_infoissue.mail_receiver_ID = bjmdr_report_unit_info.report_unit_ID "
						+ "and (bjmdr_infoissue.infoissue_ID ="
						+ infoissue_ID
						+ "OR bjmdr_infoissue.main_mail_infoissue_ID ="
						+ infoissue_ID+ ")";

				return session.createSQLQuery(sql).list();

			}
		});
	}
}
